package com.dongdongmovie.service.impl;

import com.dongdongmovie.mapper.TCollectionMapper;
import com.dongdongmovie.mapper.TMovieMapper;
import com.dongdongmovie.mapper.VipUserMapper;
import com.dongdongmovie.pojo.*;
import com.dongdongmovie.service.TCollection_Service;
import com.dongdongmovie.service.VipUser_Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@DubboService
public class TCollection_ServiceImpl implements TCollection_Service {

    //注入mapper
    @Autowired
    private TCollectionMapper tCollectionMapper;
    @Autowired
    private TMovieMapper tMovieMapper;

    //查询我的收藏
    @Override
    public PageResult<TMovie> show(Integer id, Integer pageNum, Integer pageSize) {
        TCollectionExample te = new TCollectionExample();
        TCollectionExample.Criteria criteria = te.createCriteria();
        criteria.andUserIdEqualTo(id);
        List<TCollection> tCollections = tCollectionMapper.selectByExample(te);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < tCollections.size(); i++) {
            list.add(tCollections.get(i).getMovieId());
        }
        PageHelper.startPage(pageNum, pageSize);
        TMovieExample tee = new TMovieExample();
        TMovieExample.Criteria criteria1 = tee.createCriteria();
        criteria1.andIdIn(list);
        List<TMovie> tMovies = tMovieMapper.selectByExample(tee);
        PageInfo<TMovie> pageInfo = new PageInfo<>(tMovies);
        PageResult<TMovie> pageResult = new PageResult<>(pageNum,pageSize,pageInfo.getTotal(),pageInfo.getList(),pageInfo.getPages());
        return pageResult;
    }

    //添加收藏
    @Override
    public int add(TCollection tCollection) {
        return tCollectionMapper.insertSelective(tCollection);
    }

}
